package com.first.controller;


import com.first.entity.BOrder;
import com.first.entity.BOrderWj;
import com.first.entity.Book;
import com.first.entity.User;
import com.first.service.OrderService;
import com.first.util.LayTab;
import com.first.util.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderService orders;

    private Result result = new Result();

    /**
     * 生成采购单通过前台传来的数据
     * @param books
     * @return
     */
    @RequestMapping("/generateOrder")
    public Result querById(@RequestBody ArrayList<Book> books, HttpServletRequest request) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        String orderdate =formatter1.format(date);
        int plancount = 0;
        BOrder bOrder = new BOrder();
        BOrderWj bOrderWj = new BOrderWj();
        double tprice = 0.0;
        if (books.size() > 0) {
            for (Book book : books) {
                int bookid = book.getBookid();
                int count = book.getPlancount();
                plancount += count;
                bOrderWj.setBookid(bookid);
                bOrderWj.setCount(count);
                bOrderWj.setOrderid(dateString);
                double i = orders.addOrderWj(bOrderWj);
                tprice += i;
            }
            bOrder.setB_t_price(tprice);
            bOrder.setOrderid(dateString);
            bOrder.setPlancount(plancount);
            bOrder.setOrderdate(orderdate);
            User user = (User) request.getSession().getAttribute("user");
            String  username = user.getUsername();
            bOrder.setW_name(username);
            int i = orders.addOrder(bOrder);
            if (i > 0) {
                result.setCode(1);
                result.setMsg("发布成功");
            } else {
                result.setCode(0);
                result.setMsg("发布失败");
            }
            return result;
        }else {
            result.setCode(0);
            result.setMsg("未添加数据");
            return result;
        }

    }


    /**
     * 得到订单状态为未通过的订单信息
     *
     * @param bOrder
     * @return
     */
    @RequestMapping("/failed")
    public LayTab getFailedOrder(BOrder bOrder) {
        List<Map> failedOrder = orderService.getFailedOrder(bOrder);
        LayTab layTab = new LayTab();
        if (failedOrder !=null){
            layTab.setCount(orderService.getFailedOrderCount(bOrder));
            layTab.setData(failedOrder);
        }
        return layTab;
    }


    /**
     * 查询订单状态为驳回
     *
     * @param bOrder
     * @return
     */
    @RequestMapping("/reject")
    public LayTab getRejectOrder(BOrder bOrder) {

        List<Map> failedOrder = orderService.queryReject(bOrder);
        LayTab layTab = new LayTab();
        if (failedOrder !=null){
            layTab.setCount(orderService.rejectCount(bOrder));
            layTab.setData(failedOrder);
        }
        return layTab;
    }


    /**
     * 修改驳回订单的书籍数据
     *
     * @param bOrderWjs
     * @return
     */
    @RequestMapping("/updateReject")
    public Result updateReject(@RequestBody ArrayList<BOrderWj> bOrderWjs) {

        int plancount = 0;

        if (bOrderWjs.size() > 0) {
            for (BOrderWj bOrderWj : bOrderWjs) {
                int count = bOrderWj.getCount();
                plancount += count;
                orderService.updateReject(bOrderWj);

            }

            String orderid = bOrderWjs.get(0).getOrderid();
            BOrder bOrder = new BOrder();
            bOrder.setOrderid(orderid);
            bOrder.setPlancount(plancount);
            int i = orderService.updateCount(bOrder);
            if (i > 0) {
                result.setCode(1);
                result.setMsg("修改成功");
            } else {
                result.setCode(0);
                result.setMsg("修改失败");
            }
        }
        return result;
    }

    /**
     * 订单审核通过
     *
     * @param order
     * @return
     */
    @RequestMapping("/updateStatus")
    public Result updateStatus(HttpServletRequest request, BOrder order) {
        User uesr = (User) request.getSession().getAttribute("user");
        String e_name = uesr.getUsername();
        order.setE_name(e_name);
        int i = orderService.updateStatus(order);
        Result result = new Result();
        if (i > 0) {
            result.setCode(1);
            result.setMsg("通过成功");
        } else {
            result.setCode(0);
            result.setMsg("通过失败");
        }
        return result;
    }

    /**
     * 入库申请状态改变
     * @param request
     * @param order
     * @return
     */
    @RequestMapping("/updateStatus3")
    public Result updateStatus3(HttpServletRequest request,BOrder order){
        User uesr = (User) request.getSession().getAttribute("user");
        String p_name = uesr.getUsername();
        order.setP_name(p_name);
        int i = orderService.updateStatus3(order);
        Result result = new Result();
        if(i>0){
            result.setCode(1);
            result.setMsg("通过成功");
        }else{
            result.setCode(0);
            result.setMsg("通过失败");
        }
        return result;
    }

    /**
     * 采购订单驳回
     *
     * @param request
     * @param bOrder
     * @return
     */
    @RequestMapping("/setStatus2")
    public Result setStatus2(HttpServletRequest request, BOrder bOrder) {
        User uesr = (User) request.getSession().getAttribute("user");
        String e_name = uesr.getUsername();
        bOrder.setE_name(e_name);
        int i = orderService.setStatus2(bOrder);
        Result result = new Result();
        if (i > 0) {
            result.setCode(1);
            result.setMsg("驳回成功");
        } else {
            result.setCode(0);
            result.setMsg("驳回失败");
        }
        return result;

    }
    /**
     * 重新提交订单状态 为0
     * @param bOrder
     * @return
     */
    @RequestMapping("/setStatus0")
    public Result setStatus2(BOrder bOrder) {
        int i = orderService.updateStatus0(bOrder);
        Result result = new Result();
        if (i > 0) {
            result.setCode(1);
            result.setMsg("提交成功");
        } else {
            result.setCode(0);
            result.setMsg("提交失败");
        }
        return result;

    }


    /**
     * 将订单全部执行，入库，并且将订单中的数量放入仓库
     * @param
     * @return
     */
   @RequestMapping("/Storage")
        public Result updateStatus4(BOrder bOrder){

       Date date = new Date();
       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       String dateString = formatter.format(date);
       bOrder.setStoragedate(dateString);

       int i = orderService.updateStatus4(bOrder);
       System.out.println(i);
       if (i > 0) {
           result.setCode(1);
           result.setMsg("入库成功");
       } else {
           result.setCode(0);
           result.setMsg("入库失败");
       }
       return result;
   }

    /**
     * 修改修改实际采购入库数量
     * @param wjs
     * @return
     */
   @RequestMapping("/updateCount1")
   public  Result updateCount1(@RequestBody ArrayList<BOrderWj> wjs, HttpServletRequest request){
       System.out.println(wjs);
       BOrder bOrder = new BOrder();
       BOrderWj bOrderWj = new BOrderWj();
       double price=0.0;
       int count1=0;
       String orderid="";

       if(wjs.size()>0){
           for (BOrderWj wj : wjs) {
             int  count = wj.getB_count();
                   count1 +=count;
               int b_count = wj.getB_count();
               int bookid = wj.getBookid();
               int inprice = wj.getInprice();
               orderid = wj.getOrderid();
               bOrderWj.setOrderid(orderid);
               bOrderWj.setBookid(bookid);
               bOrderWj.setB_count(b_count);
               bOrderWj.setInprice(inprice);
                double i = orderService.updateCOunt1(bOrderWj);
                price+=i;
           }
           bOrder.setActualcount(count1);
           bOrder.setOrderid(orderid);
           bOrder.setB_t_price(price);
           User user = (User) request.getSession().getAttribute("user");
           String name = user.getUsername();
           bOrder.setP_name(name);
           int i1 = orderService.updateBCount(bOrder);


           if (i1 > 0) {
               result.setCode(1);
               result.setMsg("发布成功");
           } else {
               result.setCode(0);
               result.setMsg("发布失败");
           }
       }
       return result;

   }


}
